package com.cmos.service.impl;

import com.cmos.dao.UserDao;
import com.cmos.dao.UserRoleDao;
import com.cmos.entity.User;
import com.cmos.entity.UserRole;
import com.cmos.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    //注入用户DAO
    @Resource
    private UserDao userDao;
    //注入用户-角色DAO
    @Resource
    private UserRoleDao userRoleDao;

    //新增用户
    @Override
    public int saveUser(User user) {
        return userDao.insert(user);
    }

    //删除用户
    @Override
    public int removeUser(String id) {
        return userDao.deleteByPrimaryKey(id);
    }

    //修改用户信息
    @Override
    public int changeUser(User user) {
        return userDao.updateByPrimaryKeySelective(user);
    }

    //查询用户信息
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User queryUser(User user) {
        return userDao.selectUser(user);
    }

    //查询所有用户
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<User> queryAllUser() {
        return userDao.selectAllUser();
    }

    //给用户新增角色
    @Override
    public int saveUserRole(List<UserRole> userRoles) {
        return userRoleDao.insertUserRole(userRoles);
    }

    //删除用户的角色
    @Override
    public int removeUserRole(String userId) {
        return userRoleDao.delete(new UserRole().setUserId(userId));
    }
}
